
var express = require('express');
var cookie = require('cookie-parser');
var app = express();
const util = require("./RequestUtil.js");
app.use(cookie());
app.use(express.static('html'))
const floors = ["G4000717", "G4000658", "G4000687"]
app.get("/login", function (req, res) {
    let username = req.query.username;
    let password = req.query.password;
    if (username == "yeep666" && password == "yeepxuhan") {
        res.cookie("token", "012e77c56042b9241119b2a32583cd87");
        res.redirect("/open.html");
    } else {
        res.send("password error");
    }
})
app.get("/open",async function (req, res) {

    if (req.cookies.token != "012e77c56042b9241119b2a32583cd87") {
        res.redirect("/index.html");
    } else {
        let token =  await util.login();
        let floor = req.query.floor;
        if (floors.includes(floor)) {
            let resp = await util.open(floor, token);
            res.end(resp.msg);
        }
    }
})
app.get("/getMoney", async function (req, res) {

        if (req.cookies.token != "012e77c56042b9241119b2a32583cd87") {
            res.redirect("/login.html");
        } else {
            var token =  await util.login();
                let resp = await util.getmoney(token);
                console.log(resp);
                res.end(JSON.stringify(resp.money));
        }
    });
app.listen(5000, function () {
    console.log("Server started on port 3000")
})